/**
 * Copyright (C) 2013 The yydorid-framework project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package net.yangying.androidframework.activity;

import net.yangying.androidframework.core.view.TitleBarLayout;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;

import com.actionbarsherlock.app.SherlockFragment;


public abstract class AbstractTitleSherlockFragment extends SherlockFragment implements View.OnClickListener {
	
	private static final String TAG = AbstractTitleSherlockFragment.class.getSimpleName();
	//是否提示退出
//	private boolean exitFlag = true;
	/**
	 * 退出提示持续时间
	 */
//	private static final int EXIT_TIP_DURING = 1000;
	/**
	 * 退出提示重置时间
	 */
//	private static final int EXIT_TIP_RESET = 1000;
	
//	private Handler mBaseHandler;
	private TitleBarLayout tbl;
	protected RelativeLayout titleBar;
//	private ExitTip exitTip = new ExitTip();

//	protected void onCreate(Bundle paramBundle) {
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState)
	{
		this.tbl = new TitleBarLayout(this.getActivity(), initLayout());
//		View view = inflater.inflate(this.tbl, container, false);
		View view = this.tbl;
//		super.onCreate(paramBundle);
		initUI(view);
		initListener();
//		requestWindowFeature(Window.FEATURE_NO_TITLE);
		//unavailable ,must be set MainTabActivity
//        requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
		loadData();
		return view;
	}

	public AbstractTitleSherlockFragment() {
//		mBaseHandler = new Handler() {
//			public void handleMessage(Message paramMessage) {
//				switch (paramMessage.what) {
//				case 0:
//					break;
//				default:
//					break;
//				}
//			}
//		};
	}

//	public void enableExitFlag(boolean flag){
//		this.exitFlag= flag;
//	}
	protected void handleErrorEvent(String paramString) {
//		if ((paramString != null) && (paramString.equalsIgnoreCase("-100"))) {
//			Message localMessage = this.mBaseHandler.obtainMessage();
//			localMessage.what = Integer.parseInt(paramString);
//			this.mBaseHandler.sendMessage(localMessage);
//		}
	}

	protected abstract int initLayout();
	
	protected abstract void initUI(View view);
	
	protected abstract void initListener();
	
	protected void loadData(){
		
	}
	
	protected abstract void handleTitleBarEvent(int paramInt);
	
	protected void reloadData(){
		
	}

	protected void setAppTitle(String paramString1, String paramString2) {
	}

	public void onClick(View paramView) {
		if (paramView == this.tbl.leftButton) {
			handleTitleBarEvent(0);
		} else if (paramView == this.tbl.rightButton) {
			handleTitleBarEvent(1);
		}

	}

	protected void setTitleBar(int type, String left_btntext, String title,
			String right_btntext) {
		if (this.tbl != null)
			this.tbl.setButtonTypeAndInfo(type, left_btntext, title,
					right_btntext);
	}
	
	protected void setTitleBar(int type, String title) {
		if (this.tbl != null)
			this.tbl.setButtonTypeAndInfo(type, "", title, "");
	}
	
	public void setTitleBar(String title){
		tbl.tvInfo.setText(title);
	}

	protected void setView(int layoutId,  ViewGroup container) {
//		setContentView(this.tbl);
//		View view = inflater.inflate(layoutId, container, false);
//		initUI();
//		initBaseListener();
//		initListener();
	}
	
	private void initBaseListener(){
		this.tbl.leftButton.setOnClickListener(this);
		this.tbl.rightButton.setOnClickListener(this);
	}

	public void setBaseTitle(String title){
	    this.tbl.tvInfo.setText(title);
	}
	
//	protected void setTitleProgressVisibility(boolean flag){
//	    this.tbl.setTitleProgressVisibility(flag);
//	}
	
//	protected void showToast(int resId) {
//		Toast localToast = Toast.makeText(this, resId, 0);// duration=0
//		localToast.setGravity(Gravity.CENTER, 0, 0);
//		localToast.show();
//	}

	public void setFullScreenLoadingView(){
//        Log.d(TAG, "setFullScreenLoadingView");
//        LinearLayout stubview = (LinearLayout) findViewById(R.id.Stubview);
//        stubview.setVisibility(View.VISIBLE);
//        LinearLayout parent = (LinearLayout) findViewById(R.id.loadableListHolder);
//        Log.d(TAG, "getChildCount:"+parent.getChildCount() );
//        if (parent.getChildCount() > 1) {
//            parent.removeViewAt(1);
//        }
//        parent.getChildAt(0).setVisibility(View.VISIBLE);
	}

	public void removeFullScreenLoadingView(){
//        Log.d(TAG, "removeLoadingView");
//        LinearLayout parent = (LinearLayout) findViewById(R.id.loadableListHolder);
//        parent.getChildAt(0).setVisibility(View.INVISIBLE);
	}
    
    public void setEmptyView(View view) {
//        LinearLayout stubview = (LinearLayout) findViewById(R.id.Stubview);
//        stubview.setVisibility(View.VISIBLE);
//        
//        LinearLayout parent = (LinearLayout) findViewById(R.id.loadableListHolder);
//        parent.getChildAt(0).setVisibility(View.GONE);
//        if (parent.getChildCount() > 1) {
//            parent.removeViewAt(1);
//        }
//        parent.addView(view);
//        Log.d(TAG, "setEmptyView");
    }
    public void removeStubView(){
//        LinearLayout stubview = (LinearLayout) findViewById(R.id.Stubview);
//        stubview.setVisibility(View.GONE);
//        Log.d(TAG, "removeStubView");
    }
    
//    public void setPBVisibility(boolean b) {
//        if(this.getParent()!=null)
//            this.getParent().setProgressBarIndeterminateVisibility(b);
//        else
//            setProgressBarIndeterminateVisibility(b);
//    }
    
//    protected void dialogfinish(int resId) {
//        AlertDialog.Builder builder = new AlertDialog.Builder(this);
//        builder.setMessage(getString(resId));
//        builder.setPositiveButton(getString(R.string.dialog_ok), new OnClickListener() {
//         @Override
//         public void onClick(DialogInterface dialog, int which) {
//          dialog.dismiss();
//          finish();
//         }
//        });
//        builder.setNegativeButton(getString(R.string.dialog_cancel), new OnClickListener() {
//         @Override
//         public void onClick(DialogInterface dialog, int which) {
//          dialog.dismiss();
//         }
//        });
//        builder.create().show();
//  }
//    protected void dialogExit() {
//        AlertDialog.Builder builder = new AlertDialog.Builder(this);
//        builder.setMessage(getString(R.string.dialog_exit));
//        builder.setPositiveButton(getString(R.string.dialog_ok), new OnClickListener() {
//         @Override
//         public void onClick(DialogInterface dialog, int which) {
//          dialog.dismiss();
//          Intent startMain = new Intent(Intent.ACTION_MAIN);  
//          startMain.addCategory(Intent.CATEGORY_HOME);  
//          startMain.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);  
//          startActivity(startMain);  
//          System.exit(0);
//         }
//        });
//        builder.setNegativeButton(getString(R.string.dialog_cancel), new OnClickListener() {
//         @Override
//         public void onClick(DialogInterface dialog, int which) {
//          dialog.dismiss();
//         }
//        });
//        builder.create().show();
//  }

//    protected void redirectToLoginActivity(int requestCode) {
////        Intent intent = new Intent(this, LoginActivity.class);
//        Intent intent = new Intent(this, null);
//        intent.setAction(Intent.ACTION_MAIN);
//        intent.setFlags(
//            Intent.FLAG_ACTIVITY_NO_HISTORY | 
//            Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS | 
//            Intent.FLAG_ACTIVITY_CLEAR_TOP);
////        Log.d(TAG, "requestCode:"+requestCode);
//        intent.putExtra("dd", requestCode);
//      this.startActivityForResult(intent, requestCode);
//    }
    
	/**
	 * 再按一次退出程序。
	 */
//    protected void pressAgainExit() {
//		if (exitTip.isExit()) {
//			finish();
//		} else {
//			Toast.makeText(getApplicationContext(), R.string.msg_exit_tip,EXIT_TIP_DURING).show();
//			exitTip.doExitInOneSecond();
//		}
//	}
//	class ExitTip {
//		private boolean isExit = false;
//		private Runnable task = new Runnable() {
//			@Override
//			public void run() {
//				isExit = false;
//			}
//		};
//
//		public void doExitInOneSecond() {
//			isExit = true;
//			HandlerThread thread = new HandlerThread("doTask");
//			thread.start();
//			new Handler(thread.getLooper()).postDelayed(task, EXIT_TIP_RESET);
//		}
//		
//		public boolean isExit() {
//			return isExit;
//		}
//
//		public void setExit(boolean isExit) {
//			this.isExit = isExit;
//		}
//	}
//	
//	@Override
//	public boolean onKeyDown(int keyCode, KeyEvent event) {
//		if(exitFlag){
//			if (keyCode == KeyEvent.KEYCODE_BACK) {
//				pressAgainExit();
//				return true;
//			}	
//		}
//		return super.onKeyDown(keyCode, event);
//	}

}
